;; TODO: this isn't a proper README at all!
;; TODO: guix package definition

;; SPDX-FileCopyrightText: 2022 Maxime Devos <maximedevos@telenet.be>
;;
;; SPDX-License-Identifier: AGPL-3.0-or-later

;; This module provides an interface to the GNUnet DHT service for storing and
;; retrieving blocks. This can be used to store blocks of ERIS encoded content.
;; TODO: the FS service has some fanciness w.r.t. reputation and not storing
;; everything on DHT directly, instead contacting relevant peers with CADET.
;;
;; Scheme-GNUnet is AGPL, so this module is AGPL (and not GPL) too.

(use-modules (eris) (eris blocks gnunet-dht) (rnrs io ports)
             (fibers) (fibers conditions))

(define (test)
  (define config (load-configuration))
  (define (connected)
    (pk 'haai))
  (parameterize ((eris-gnunet-dht-server
                  (connect config #:connected connected)))
    ;; Put something into the DHT
    (define eris-urn
      (eris-encode "Hello world!" #:block-reducer
                   eris-blocks-gnunet-dht-reducer))
    ;; Retrieve it
    ;; XXX: "Attempt to suspend fiber within continuation barrier"
    ;; --> offload to a separate thread
    ;;
    ;; Long term, it would be nice if make-custom-binary-input-port
    ;; and friends didn't have this problem ...
    (define decoded)
    (define done-decoding (make-condition))
    ((@ (ice-9 threads) call-with-new-thread)
     (lambda ()
       (set! decoded
         (eris-decode->bytevector eris-urn
                                  #:block-ref eris-blocks-gnunet-dht-ref))
       (signal-condition! done-decoding)))
    (wait done-decoding)
    (pk 'decoded (utf8->string decoded))))

(export test)
(run-fibers test)
